home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / minit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-16  |  5.7 KB  |  254 lines

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #ifndef NOMALLOC_H
  5. #include <malloc.h>
  6. #endif
  7. #ifndef NOUNISTD_H
  8. #include <unistd.h>
  9. #endif
  10.  
  11. #include "fudgit.h"
  12. #include "setshow.h"
  13. #include "macro.h"
  14. #include "head.h"
  15.  
  16. static struct {
  17.     char     *name;
  18.     char     *command;
  19.     int        nargs;
  20. } com[] = {
  21.     {"gnu!plot", "\tsave vector $1 $2 $Tmp\n\tpmode plot '$Tmp'\n", 2},
  22. /* add built-in macros here: e.g., 
  23.  * "sgi!plot",
  24.  *    "\tpmode read -\n\t_dumplot $1 $2\n\tpmode\n\tdone\n\tplot\n\tfmode\n", 2,
  25.  */
  26.     {0,      0,        0}
  27. };
  28.  
  29. static struct {
  30.     char *name;
  31.     char *alias;
  32. } aliases[] = {
  33.     {"rm",  "!rm -i\n"},
  34.     {"cp", "!cp -i\n"},
  35.     {"mv", "!mv -i\n"},
  36.     {"da!te", "!date\n"},
  37.     {0, 0}
  38. };
  39.  
  40.  
  41.  
  42. extern int Ft_almost (register char *str1, register char *str2);
  43. extern FILE *popen(const char *, const char *);
  44.  
  45. void Ft_initmacros(void)
  46. {
  47.     int i;
  48.     Macro *m;
  49.  
  50.     for (i = 0;aliases[i].name; i++) {
  51.         m = Ft_macinstall(aliases[i].name, aliases[i].alias, ANALIAS);
  52.     }
  53.     for (i = 0;com[i].name; i++) {
  54.         m = Ft_macinstall(com[i].name, com[i].command, AMACRO);
  55.         m->nargs = com[i].nargs;
  56.     }
  57. }
  58.  
  59. static Macro *maclist = 0;
  60.  
  61. Macro *Ft_mplisp(void)
  62. {
  63.     return(maclist);
  64. }
  65.  
  66. Macro *Ft_maclook(char *m, int type)
  67. {
  68.     Macro *mp;
  69.  
  70.     if (!m)
  71.         return(NULL);
  72.     for (mp = maclist; mp != (Macro *)0; mp = mp->next) {
  73.         if (Ft_almost(m, mp->name) && mp->type == type)
  74.             return(mp);
  75.     }
  76.     return(NULL);
  77. }
  78.  
  79. int Ft_save_macros(int type, char *name, char *mode)
  80. {
  81.     Macro *mp;
  82.     FILE *fp;
  83.  
  84.     if ((fp = fopen(name, mode)) == NULL) {
  85.         fprintf(stderr, "%s: Permission denied.\n", name);
  86.         return(ERRR);
  87.     }
  88.     for (mp = maclist; mp != (Macro *)0; mp = mp->next) {
  89.         if (mp->type != type)
  90.             continue;
  91.         fprintf(fp, "%c\n", Ft_Comchar);
  92.         switch (mp->type) {
  93.             case AMACRO:
  94.             fprintf(fp, "define %s %s %d\n", "macro", mp->name, mp->nargs);
  95.             fprintf(fp, "%sstop\n", mp->line);
  96.             break;
  97.             default:
  98.             fprintf(stderr, "Impossible macro type in save_macro().\n");
  99.             return(ERRR);
  100.         }
  101.     }
  102.     fclose(fp);
  103.     return(0);
  104. }
  105.  
  106. Macro *Ft_macinstall(char *name, char *line, int type)
  107. {
  108.     Macro *mp;
  109.     unsigned int size = TOKENSIZE;
  110.  
  111.     if ((mp = (Macro *)malloc(sizeof(Macro))) == (Macro *)0) {
  112.         fprintf(stderr, "Allocation error in macro install.\n");
  113.         Ft_catcher(ERRR);
  114.     }
  115.     if ((mp->name = (char *)malloc(strlen(name)+1)) == (char *)0) {
  116.         fprintf(stderr, "Allocation error in macro install.\n");
  117.         Ft_catcher(ERRR);
  118.     }
  119.     if (type == AMACRO || type == ANALIAS) {
  120.         char *fp, *np;
  121.  
  122.         if ((mp->fname = (char *)malloc(strlen(name)+1)) == (char *)0) {
  123.             fprintf(stderr, "Allocation error in macro install.\n");
  124.             Ft_catcher(ERRR);
  125.         }
  126.         for (np=name, fp=mp->fname; *np != '\0'; np++) {
  127.             if (*np != '!')
  128.                 *fp++ = *np;
  129.         }    
  130.         *fp = '\0';
  131.         size = strlen(line) + 1;
  132.     }
  133.     else {
  134.         mp->fname = 0;
  135.     }
  136.     if ((mp->line = (char *)malloc(size)) == (char *)0) {
  137.         fprintf(stderr, "Allocation error in macro install.\n");
  138.         Ft_catcher(ERRR);
  139.     }
  140.  
  141.     mp->type = type;
  142.     mp->nargs = 0;
  143.     if (*name)
  144.         sscanf(name, "%s", mp->name);
  145.     if (*line)
  146.         sprintf(mp->line, "%s", line);
  147.     mp->next = maclist;
  148.     maclist = mp;
  149.     return(mp);
  150. }
  151.  
  152.     /* remove macros on request */
  153. int Ft_macremove(char *name, int type)
  154. {
  155.     Macro *mp, *mprev;
  156.     Macro *mpres;
  157.     int found;
  158.     int all;
  159.    
  160.     found = all = (strcmp(name, "@all") == 0);
  161.     mp = maclist;
  162.     while (mp != (Macro *)0) {
  163.         if ((Ft_almost(name, mp->name) || all) && mp->type == type) {
  164.             found = 1;
  165.             mpres = mp;
  166.             if (mp == maclist) {  /* first one ? */
  167.                 maclist = mp->next;
  168.                 mp = maclist;
  169.             }
  170.             else {
  171.                 mp = mprev->next = mp->next;
  172.             }
  173.             free(mpres->name);
  174.             free(mpres->line);
  175.             free((char *)mpres);
  176.         }
  177.         else {
  178.             mprev = mp;
  179.             mp = mp->next;
  180.         }
  181.     }
  182.  
  183.     if (!found) {
  184.         if (type == AMACRO)
  185.             fprintf(stderr, "Warning: %s: No such macro.\n", name);
  186.         else
  187.             fprintf(stderr, "Warning: %s: No such alias.\n", name);
  188.         return(ERRR);
  189.     }
  190.     return(0);
  191. }
  192.  
  193. int Ft_showmac(int argc, char **argv, int type)
  194. {
  195.     FILE *fp;
  196.     Macro *mp;
  197.     int found, some;
  198.     extern int Ft_Interact;
  199.  
  200.     fp = stdout;
  201.     some = (argc == 3);
  202.     found = 0;
  203. #ifndef AMIGA
  204.     if (!some && Ft_Interact && *Ft_Pager && type == AMACRO) {
  205.         if ((fp = popen(Ft_Pager, "w")) == (FILE *)NULL)  {
  206.             fprintf(stderr, "Could not open pager %s.\n", Ft_Pager);
  207.             fp = stdout;
  208.         }
  209.     }
  210. #endif
  211.  
  212.     for (mp = maclist; mp != (Macro *)0; mp = mp->next) {
  213.         if (mp->type != type)
  214.             continue;
  215.         if (some) {
  216.             if (found) {
  217.                 if (fp != stdout) {
  218.                     pclose(fp);
  219.                 }
  220.                 return(0);
  221.             }
  222.             else if (Ft_almost(argv[2], mp->name)) {
  223. #ifndef AMIGA
  224.                 if ((fp = popen(Ft_Pager, "w")) == (FILE *)NULL)  {
  225.                     fprintf(stderr, "Could not open pager %s.\n", Ft_Pager);
  226.                     fp = stdout;
  227.                 }
  228. #endif
  229.                 found = 1;
  230.             }
  231.             else {
  232.                 continue;
  233.             }
  234.         }
  235.         switch (mp->type) {
  236.             case ANALIAS:
  237.                 fprintf(fp, "%s\t%s",
  238.                 mp->name, mp->line);
  239.                 break;
  240.             case AMACRO:
  241.                 fprintf(fp, "macro %s: (%d args)\n{\n%s}\n",
  242.                 mp->name, mp->nargs, mp->line);
  243.                 break;
  244.             default:
  245.                 break;
  246.         }
  247.     }
  248.     if (some && !found)
  249.         fprintf(stderr, "show macro: %s: No such macro.\n", argv[2]);
  250.     if (fp != stdout)
  251.         pclose(fp);
  252.     return(0);
  253. }
  254.